package com.example.archermind.myapplication;

import android.app.PendingIntent;
import android.app.Popup;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddPopUpActivity extends BaseActivity {


    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.et_icon)
    EditText etIcon;
    @BindView(R.id.et_notificationid)
    EditText etNotificationid;
    @BindView(R.id.et_packageName)
    EditText etPackageName;
    @BindView(R.id.et_priority)
    EditText etPriority;
    @BindView(R.id.et_longitude)
    EditText etLongitude;
    @BindView(R.id.et_latitude)
    EditText etLatitude;
    @BindView(R.id.btn_addPopup)
    Button btnAddPopup;
    @BindView(R.id.et_actionOne)
    EditText etActionOne;
    @BindView(R.id.et_actionOneText)
    EditText etActionOneText;
    @BindView(R.id.et_actionTwo)
    EditText etActionTwo;
    @BindView(R.id.et_actionTwoText)
    EditText etActionTwoText;
    @BindView(R.id.et_actionCancel)
    EditText etActionCancel;
    @BindView(R.id.et_actionCancelText)
    EditText etActionCancelText;
    @BindView(R.id.et_iconRefId)
    EditText etIconRefId;
    @BindView(R.id.tv_onClickNotification)
    TextView tvOnClickNotification;
    @BindView(R.id.tv_onDelNotification)
    TextView tvOnDelNotification;

    Handler mHdler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            String notificationid = bundle.getString("notificationid");
            switch (msg.what) {
                case NOTIFICATIONCALLBACK + 0:
                    tvOnClickNotification.setText("OnClickNotification notificationid:"+notificationid);
                    break;
                case NOTIFICATIONCALLBACK + 1:
                    tvOnDelNotification.setText("OnDelNotification notificationid:"+notificationid);
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pop_up);
        unbinder = ButterKnife.bind(this);
        setHandler(mHdler);
    }

    @OnClick(R.id.btn_addPopup)
    public void onViewClicked(View view) {
        Log.d(TAG, "add popup");
        try {
//{
//            "headLine":"事项提醒",
//                    "text":"今天下午3：00去交物业费，不用忘记哦！",
//                    "iconRefId":1,
//                    "actionOne ":"Calendar页面详情",
//                    "actionOneText":"确定",
//                    "actionTwo":"",
//                    "actionTwoText":"",
//                    "actionCancel":"",
//                    "actionCancelText":"取消",
//                    "timestamp":1489026967,
//                    "priority ":0,
//                    "longitude":-1.0,
//                    "latitude":-1.0
//        }
            String notificationId = etNotificationid.getText().toString();
            String packageName = etPackageName.getText().toString();
            String icon = etIcon.getText().toString();
            String title = etTitle.getText().toString();
            String content = etContent.getText().toString();
            String priority = etPriority.getText().toString();
            String longitude = etLongitude.getText().toString();
            String latitude = etLatitude.getText().toString();
            String actionOne = etActionOne.getText().toString();
            String actionOneText = etActionOneText.getText().toString();
            String actionTwo = etActionTwo.getText().toString();
            String actionTwoText = etActionTwoText.getText().toString();
            String actionCancel = etActionCancel.getText().toString();
            String actionCancelText = etActionCancelText.getText().toString();
            String iconRefId = etIconRefId.getText().toString();
            Popup popup = new Popup(title, content);
            popup.setPriority(Integer.parseInt(priority));
            popup.setLatitude(Double.parseDouble(latitude));
            popup.setLongitude(Double.parseDouble(longitude));
            Intent intent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            popup.setActionOne(actionOneText, pendingIntent);
            popup.setActionCancel(actionCancelText, actionCancel);
            popup.setActionTwo(actionTwoText, actionTwo);
            popup.setIcon(Integer.parseInt(iconRefId));
//            StringBuffer stringBuffer = new StringBuffer("{headLine:" + title + ",");
//            stringBuffer.append("text:" + content+",");
//            stringBuffer.append("iconRefId:" + iconRefId+",");
//            stringBuffer.append("actionOne:" + actionOne+",");
//            stringBuffer.append("actionOneText:" + actionOneText+",");
//            stringBuffer.append("actionTwo:" + actionTwo+",");
//            stringBuffer.append("actionTwoText:" + actionTwoText+",");
//            stringBuffer.append("actionCancel:" + actionCancel+",");
//            stringBuffer.append("actionCancelText:" + actionCancelText+",");
//            stringBuffer.append("timestamp:" + System.currentTimeMillis()+",");
//            stringBuffer.append("priority:" + priority+",");
//            stringBuffer.append("longitude:" + longitude+",");
//            stringBuffer.append("latitude:" + latitude);
//            stringBuffer.append("}");
            Log.d(TAG, "addPopup:notificationId:" + notificationId + " packageName:" + packageName + " icon:" + icon + " paramsJson" + buildPopupParam(popup));
            getNotificationService().addPopup(notificationId, packageName, icon, buildPopupParam(popup));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private String buildPopupParam(Popup pop) {
        JSONObject json = new JSONObject();
        try {
            json.put("headLine", pop.getTitle());
            json.put("text", pop.getContent());
            json.put("iconRefId", pop.getIcon());
            if (pop.getActionOne() != null) {
                json.put("actionOne", pop.getActionOne().toString());
            } else {
                json.put("actionOne", "");
            }
            json.put("actionOneText", pop.getActionOneText());
            json.put("actionTwo", pop.getActionTwo());
            json.put("actionTwoText", pop.getActionTwoText());
            json.put("actionCancel", pop.getActionCancel());
            json.put("actionCancelText", pop.getActionCancelText());
            json.put("timestamp", pop.getTimestamp());
            json.put("priority", pop.getPriority());
            json.put("longitude", pop.getLongitude());
            json.put("latitude", pop.getLatitude());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

}
